package com.itheima.contorller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CategoryException;
import com.itheima.common.R;
import com.itheima.domain.*;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;
    //新增功能
     @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
       setmealService.saveSetmeal(setmealDto);
       return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        Page<Setmeal> dishpage = new Page<>(page,pageSize);
        Page<SetmealDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> dishpageLambdaQueryWrapper = new LambdaQueryWrapper<>();

        dishpageLambdaQueryWrapper.like(Strings.isNotEmpty(name),Setmeal::getName,name);
        dishpageLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(dishpage,dishpageLambdaQueryWrapper);
        BeanUtils.copyProperties(dishpage,dishDtoPage,"records");

        List<Setmeal> records = dishpage.getRecords();

        List<SetmealDto> list= records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            String name1 = byId.getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;

        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);

    }
    //根据id回显数据
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto byIdSetmeal = setmealService.getByIdSetmeal(id);
        return R.success(byIdSetmeal);
    }
    //保存数据
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
         setmealService.updateSetmeal(setmealDto);
         return R.success("提交成功");
    }

    //禁用和启用
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(ids != null,Setmeal::getId,ids);
        List<Setmeal> list = setmealService.list(dishLambdaQueryWrapper);

        for (Setmeal setmeal : list) {
            if (setmeal!=null){
                setmeal.setStatus(status);
                setmealService.updateById(setmeal);
            }

        }
        return R.success("修改成功");


    }
    //删除
    @DeleteMapping
    public R<String> deleteById(Long[] ids){


        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
            setmealLambdaQueryWrapper.in(Setmeal::getId,id);
            int count = setmealService.count(setmealLambdaQueryWrapper);
            if (count>0){
                throw new CategoryException("套餐正在售卖中，不能删除");
            }


            setmealService.removeById(id);
            LambdaQueryWrapper<SetmealDish> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(dishFlavorLambdaQueryWrapper);
        }
        return R.success("删除成功");
    }
    //用户端根据id查询餐品套餐
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> dishtLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishtLambdaQueryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        dishtLambdaQueryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(dishtLambdaQueryWrapper);

     List<SetmealDto> listt= list.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long id = item.getId();
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> list1 = setmealDishService.list(setmealDishLambdaQueryWrapper);
             setmealDto.setSetmealDishes(list1);
             return setmealDto;
        }).collect(Collectors.toList());

        return R.success(listt);
    }

    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dish(@PathVariable("id") Long SetmealId) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, SetmealId);
        //获取套餐里面的所有菜品  这个就是SetmealDish表里面的数据
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        List<DishDto> dishDtos = list.stream().map((setmealDish) -> {
            DishDto dishDto = new DishDto();
            //其实这个BeanUtils的拷贝是浅拷贝，这里要注意一下
            BeanUtils.copyProperties(setmealDish, dishDto);
            //这里是为了把套餐中的菜品的基本信息填充到dto中，比如菜品描述，菜品图片等菜品的基本信息
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            BeanUtils.copyProperties(dish, dishDto);

            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtos);
    }
    }
